using System;
using Microsoft.Xna.Framework;

namespace XDL.Framework.Particles
{
    /// <summary>
    /// Helper for objects that want to leave particles behind them as they
    /// move around the world. This emitter implementation solves two related
    /// problems:
    /// 
    /// If an object wants to create particles very slowly, less than once per
    /// frame, it can be a pain to keep track of which updates ought to create
    /// a new particle versus which should not.
    /// 
    /// If an object is moving quickly and is creating many particles per frame,
    /// it will look ugly if these particles are all bunched up together. Much
    /// better if they can be spread out along a line between where the object
    /// is now and where it was on the previous frame. This is particularly
    /// important for leaving trails behind fast moving objects such as rockets.
    /// 
    /// This emitter class keeps track of a moving object, remembering its
    /// previous position so it can calculate the velocity of the object. It
    /// works out the perfect locations for creating particles at any frequency
    /// you specify, regardless of whether this is faster or slower than the
    /// game update rate.
    /// </summary>
    public class ParticleEmitter : 
        Emitter
    {
        ParticleSystem m_particleSystem;
        /// <summary>
        /// Constructs a new particle emitter object.
        /// </summary>
        public ParticleEmitter(ParticleSystem particleSystem,
                               float particlesPerSecond):
            base(particlesPerSecond)
        {
            m_particleSystem = particleSystem;
        }

        /// <summary>
        /// Constructs a new particle emitter object.
        /// </summary>
        public ParticleEmitter(ParticleSystem particleSystem,
                               float particlesPerSecond, 
                               Vector3 position,
                               Vector3 velocity)
            :
            base(particlesPerSecond, position, velocity)
        {
            m_particleSystem = particleSystem;
        }

        /// <summary>
        /// Returns the underlying <see cref="ParticleSystem"/> instance.
        /// </summary>
        public ParticleSystem ParticleSystem
        {
            get { return m_particleSystem; }
        }

        protected override void AddParticle(Vector3 position, Vector3 velocity)
        {
            // Create the particle.
            m_particleSystem.AddParticle(position, velocity);
        }
    }
}
